#!/bin/bash
# setup augmavconn and libmavconn environment from git repo
# Author Lai Jun, <laijun@nudt.edu.cn>
# 2017.5.21
needCompile=true # if set to true, this script will build
publishVersion=v2.1 
LibmavconnTag=v0.1
augmavconnTag=v2.1
CommunicationTag=v2
LibmavconnGit='https://git.oschina.net/smarteyes-comm/libmavconn.git'
augmavconnGit='https://git.oschina.net/smarteyes-comm/augmavconn.git'
CommunicationGit='https://git.oschina.net/smarteyes-comm/communication.git'

p=$(pwd)
WS_ROOT=$p/communication_ws/catkin_libaugmavconn_ws
echo "Current folder: $p"
echo "Will create workspace in $WS_ROOT"
if [ -e $WS_ROOT ]
then
  echo "There already is expected folder, exit"
else 
    mkdir -p $WS_ROOT/src
    cd $WS_ROOT/src
    git clone $LibmavconnGit
    git clone $augmavconnGit

    if [ ! -e $WS_ROOT/src/libmavconn -o ! -e $WS_ROOT/src/augmavconn ]
    then 
        echo "Seems you can't download the repos correctly, check your Internet connection or your permission"
    else
        cd libmavconn
        git checkout $LibmavconnTag
        cd ../
        cd augmavconn 
        git checkout $augmavconnTag
        cd ../

        if [ "$needCompile" = true ]
        then
          cd ../
          catkin_make

          if [ $? -eq 0 ]
          then 
            source ./devel/setup.bash 
            echo "enjoy"
            echo "Besides, if you can't find libmavconn and augmavconn correctly through roscd, change your work folder, and try again. Only god kown what happens."

            cd $p 
            WS_ROOT_Com=$p/communication_ws/catkin_communication_ws
            if [ -e $WS_ROOT_Com ]
            then
              echo "There already is expected folder, exit"
            else 
              mkdir -p $WS_ROOT_Com/src
              cd $WS_ROOT_Com/src
              git clone $CommunicationGit
              
              if [ ! -e $WS_ROOT_Com/src/communication ]
                then 
                    echo "Seems you can't download the repos correctly, check your Internet connection or your permission"
                else
                    cd communication
                    git checkout $CommunicationTag
                    cd ../  
                    cd ../
                    catkin_make

                    if [ $? -eq 0 ]
                    then 
                      source ./devel/setup.bash 
                      echo "enjoy your communication, you can just type roslaunch blah blah"
                      echo "Besides, if you can't find libmavconn and augmavconn correctly through roscd, change your work folder, and try again. Only god kown what happens."
                    else
                      echo "Something is wrong"
                    fi
                fi 
              fi 
          else
            echo "Something is wrong"
          fi
        else
            WS_ROOT_Com=$p/communication_ws/catkin_communication_ws
            if [ -e $WS_ROOT_Com ]
            then
              echo "There already is expected folder, exit"
            else 
              mkdir -p $WS_ROOT_Com/src
              cd $WS_ROOT_Com/src
              git clone $CommunicationGit
              if [ ! -e $WS_ROOT_Com/src/communication ]
                then 
                    echo "Seems you can't download the repos correctly, check your Internet connection or your permission"
                else
                    cd communication
                    git checkout $CommunicationTag
                    cd ../ 
                    echo "enjoy your communication, you can just type make in your workspace"
                    echo "Besides, if you can't find libmavconn and augmavconn correctly through roscd, change your work folder, and try again. Only god kown what happens."
                fi 
            fi
        fi

        # copy readme and makefile
        if [ ! -e $p/makefile ]
        then
          echo "makefile doesn't exists! make sure you have makefile in current folder. However, you can still use this workspace"
        else
          cp $p/makefile $p/communication_ws
        fi
        if [ ! -e $p/workspacereadme.md ]
        then
          echo "workspacereadme.md doesn't exists! make sure you have makefile in current folder. However, you can still use this workspace"
        else
          cp $p/workspacereadme.md $p/communication_ws/README.md
        fi
        
        echo "version: $publishVersion" >> $p/communication_ws/packetversion
        echo "libmavconn: $LibmavconnTag" >> $p/communication_ws/packetversion
        echo "augmavconn: $augmavconnTag" >> $p/communication_ws/packetversion
        echo "communication: $CommunicationTag" >> $p/communication_ws/packetversion
    fi 
fi

cd $p
